<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <base href="#(basePath)"/>
  <title>清算管理 - 付款数据</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="static/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="static/admin.css" media="all">
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-form layui-card-header layuiadmin-card-header-auto">

          <div class="layui-inline">
            <label class="layui-form-label">开始时间</label>
            <div class="layui-input-inline">
              <input type="text" class="layui-input" value="#(startTime)" id="createTimeStart" name="createTimeStart" placeholder="yyyy-MM-dd HH:mm:ss">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">结束时间</label>
            <div class="layui-input-inline">
              <input type="text" class="layui-input" value="#(endTime)" id="createTimeEnd" name="createTimeEnd" placeholder="yyyy-MM-dd HH:mm:ss">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
              <select name="orderStatus" lay-filter="orderStatus"  disabled="disabled">
                <option value="WT">待打款</option>
              </select>
            </div>
          </div>

            <div class="layui-inline">
                <label class="layui-form-label">对公对私</label>
                <div class="layui-input-inline">
                    <select name="dgds"  lay-filter="bizType">
                        <option value="1" selected>对公</option>
                        <option value="2">对私</option>

                    </select>
                </div>
            </div>


          <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-web-list-search">
              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
              #auth()
              <button link="admin/clearing/payexport/export" class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="exporttk">导出待打款数据表</button>
              #end

              #auth()
              <button link="admin/pay/toTransferOutList" class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="moreok">批量打款成功</button>
              #end
          </div>
        </div>

      <div class="layui-card-body">
        <table id="LAY-web-list" lay-filter="LAY-web-list"></table>
        <script type="text/html" id="table-content-list">
        </script>
      </div>
    </div>
  </div>

  <script src="static/layui/layui.js"></script>
  <script>
  layui.config({
    base: '#(basePath)' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'laydate', 'table', 'multiSelect',   'payexport'], function(){
	var laydate = layui.laydate, multiSelect = layui.multiSelect;
    //日期时间选择器
    laydate.render({
      elem: '#createTimeStart'
      ,type: 'datetime'
    });
    laydate.render({
      elem: '#createTimeEnd'
      ,type: 'datetime'
    });

    // 表格
    var table = layui.table
    ,form = layui.form;

    //监听搜索
    form.on('submit(LAY-web-list-search)', function(data){
      var field = data.field;

        // var checkStatus = table.checkStatus('idOrderList'); //idTest 即为基础参数 id 对应的值
        // console.log(checkStatus.data); //获取选中行的数据
        // console.log(checkStatus.data.length); //获取选中行数量，可作为是否有选中行的条件
        // console.log(checkStatus.isAll ); //表格是否全选

        field.bizType2="33355";
        field.bizTypex="";
        console.log(field);

        var vals = [],
            texts = [];
        var tt = 1;
        $('select[multiple] option:selected').each(function() {
            field.bizTypex += $(this).val() + ";";
            vals.push($(this).val());
            texts.push($(this).text());
        });
        console.dir(vals);
        console.dir(texts);


      //执行重载
      table.reload('LAY-web-list', {
        where: field,
        page: {curr: 1}
      });
    });


      //判断字符是否为空的方法
      function isEmpty(obj){
          if(typeof obj == "undefined" || obj == null || obj == ""){
              return true;
          }else{
              return false;
          }
      }
    form.on('submit(exporttk)', function (data) {
        var valsel = [];
        valsel.push("df_company");
        valsel.push("df_personal");
        valsel.push("withdraw_company");
        valsel.push("withdraw_personal");

        var field = data.field;

        field.orderStatus = "WT";
        console.log(field);

        var createTimeStart = $("#createTimeStart").val();
        var createTimeEnd = $("#createTimeEnd").val();
        if(isEmpty(createTimeStart) || isEmpty(createTimeEnd)){
            layer.alert("导出Excel必须选择开始时间和结束时间，并且时间段建议不超过3个月!");
            return;
        }
        layer.msg("正在导出Excel表...");

        $.ajax({
            type:"POST",
            url:"admin/clearing/payexport/report",
            data:field,
            dataType: "JSON",
            success: function(data) {
                //alert(JSON.stringify(data));
                if(data.code==0){
                    window.location.href= data.msg;
                    console.log(JSON.stringify(data));
                    $("#moreOK").attr("style", "display: block");
                }else {
                    layer.alert(JSON.stringify(data));
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                layer.alert("网络错误");
            }
        });

        field.orderStatus = "WT";

        //执行重载
        table.reload('LAY-web-list', {
            where: field,
            page: {curr: 1}
        });

    });

   function toPayListOK(checkStatus, field){
       var param ="num=" + checkStatus.data.length +  "&orderNoList=";
       for(var uu = 0; uu < checkStatus.data.length; uu++ ){
           var bizType =  checkStatus.data[uu].bizType;
           var orderStatus = checkStatus.data[uu].orderStatus;
           if('WT' != orderStatus){
               layer.alert("请选择待对公或者对私的待打款的数据,订单状态需要为[待打款]");
               return;
           }
           if('transfer_out' == bizType){
               layer.alert("请选择待对公或者对私的待打款的数据,钱包转账不支持批量操作!");
               return;
           }
           param += checkStatus.data[uu].orderNo + ";";
       }
       {
           // var param = "purpose4=批量操作成功&reviewResult=pass&id=" +  checkStatus.data[uu].id;
           // var orderNo =  checkStatus.data[uu].orderNo;
           $.ajax({
               type: "POST",
               url: "admin/pay/toTransferOutList",
               data: param,
               dataType: "JSON",
               success: function (data) {
                   if(data.code==0){
                       layer.alert( "打款成功:" + data.msg);
                   }else {
                       layer.alert("打款失败:" + data.msg);
                   }
                   //执行重载
                   table.reload('LAY-web-list', {
                       where: field,
                       page: {curr: 1}
                   });
               },
               error: function (data) {
                   layer.alert("网络错误");
               }
           });
       }
   }

    form.on('submit(moreok)', function (data) {
        var field = data.field;
        var checkStatus = table.checkStatus('LAY-web-list'); //idTest 即为基础参数 id 对应的值
        console.log(checkStatus.data); //获取选中行的数据
        console.log(checkStatus.data.length); //获取选中行数量，可作为是否有选中行的条件
        console.log(checkStatus.isAll ); //表格是否全选

        if(checkStatus.data.length <= 0){
            layer.alert("请选择待对公或者对私的打款的数据!");
            return;
        }
        layer.open({
            type : 1,
            title : "确定付款完成",
            content : "确定选择的订单都已经付款完成?",
            maxmin : !0,
            area : [ '70%', '90%' ],
            btn : [ "付款完成", "取消" ],
            yes : function(index, layero) {
                toPayListOK(checkStatus, field);
                layer.close(index);
            }
        });
    });



    var $ = layui.$,
    active = {
      add: function(){
  		parent.layer.open({
          type: 2
          ,title: '添加'
          ,content: 'admin/pay/addPage'
          ,maxmin: true
          ,area: ['80%', '90%']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
             //点击确认触发 iframe 内容中的按钮提交
             layero.find('iframe').contents().find("#layuiadmin-app-form-submit").click();
           }
        });
      }
    };

    $('.layui-btn.layuiadmin-btn-list').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
  </script>
</body>
</html>
